using System;
using System.Collections.Generic;
using System.Text;
using Jbe.TestSuite.TestDevices.FunctionGenerator.Interface;
using Jbe.TestSuite.Infrastructure.Interface;
using System.ComponentModel;
using Jbe.Composite.Extensions;
using System.Reflection;

namespace Jbe.TestSuite.TestDevices.FunctionGenerator.Driver.Domain
{
    public class FunctionGeneratorConfig : Model, IFunctionGeneratorConfig
    {
        private bool readOnly = false;
        private WaveForm waveForm;
        private double amplitude;
        private double frequency;


        public bool IsReadOnly
        {
            get { return readOnly; }
        }
        
        public WaveForm WaveForm
        {
            get { return waveForm; }
            set 
            {
                if (readOnly) { throw new InvalidOperationException("Object is readonly."); }
                if (waveForm != value)
                {
                    waveForm = value;
                    RaisePropertyChanged("WaveForm");
                }
            }
        }

        public double Amplitude
        {
            get { return amplitude; }
            set 
            {
                if (readOnly) { throw new InvalidOperationException("Object is readonly."); }
                if (amplitude != value)
                {
                    if (value < 0 || value > 1e3)
                    {
                        throw new ArgumentOutOfRangeException("Amplitude", value, "The amplitude must be between 0 and 1000 V.");
                    }
                    amplitude = value;
                    RaisePropertyChanged("Amplitude");
                }
            }
        }

        public double Frequency
        {
            get { return frequency; }
            set 
            {
                if (readOnly) { throw new InvalidOperationException("Object is readonly."); }
                if (frequency != value)
                {
                    if (value < 0.5 || value > 5e6)
                    {
                        throw new ArgumentOutOfRangeException("Frequency", value, "The frequency must be between 0.5 and 5 000 000 Hz.");
                    }
                    frequency = value;
                    RaisePropertyChanged("Frequency");
                }
            }
        }


        public void Lock()
        {
            if (!readOnly)
            {
                readOnly = true;
                RaisePropertyChanged("IsReadOnly");
            }
        }

        public IFunctionGeneratorConfig Clone()
        {
            FunctionGeneratorConfig config = new FunctionGeneratorConfig();
            config.readOnly = false;
            config.waveForm = waveForm;
            config.amplitude = amplitude;
            config.frequency = frequency;
            return config;
        }
    }
}
